package com.conv.shop.business.service;

import cn.hutool.core.date.DateTime;
import com.conv.shop.business.domain.ShoppingCart;
import com.conv.shop.business.domain.ShoppingCartExample;
import com.conv.shop.business.mapper.ShoppingCartMapper;
import com.conv.shop.business.req.ShoppingCartReq;
import com.conv.shop.business.vo.CarVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private ConsumerService consumerService;

    // 根据用户id查询购物车
//    public List<ShoppingCart> getList(String phone){
//        ShoppingCartExample shoppingCartExample = new ShoppingCartExample();
//        int consumerId = consumerService.selectByMobile(phone).getConsumerId();
//        shoppingCartExample.createCriteria().andConsumerIdEqualTo(consumerId);
//        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByExample(shoppingCartExample);
//        return shoppingCartList;
//    }

    /**
     * 联表查询购物车和商品
     * @param phone
     * @return List<CarVo>
     */
    public List<CarVo> getList(String phone){
        ShoppingCartExample shoppingCartExample = new ShoppingCartExample();
        int consumerId = consumerService.selectByMobile(phone).getConsumerId();
        List<CarVo> carVoList = shoppingCartMapper.carVoList(consumerId);
        return carVoList;
    }

    /**
     * 添加至购物车
      * @param req
     * @return boolean
     */

    public boolean save(ShoppingCartReq req){
        Date now = DateTime.now();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setConsumerId(req.getConsumerId());
        shoppingCart.setNumber(req.getNumber());
        shoppingCart.setSelected(req.getSelected());
        shoppingCart.setAddToCartTime(now);
        shoppingCart.setLastUpdateTime(now);
        shoppingCart.setProductId(req.getProductId());
        int i = shoppingCartMapper.insert(shoppingCart);
        if (i!=0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除购物车
     * @param shoppingCartId
     * @return boolean
     */
    public boolean delete(Integer shoppingCartId){
        int i = shoppingCartMapper.deleteByPrimaryKey(shoppingCartId);
        if (i != 0){
            return true;
        }else {
            return false;
        }

    }
}
